<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to='/'>首页</XtxBreadItem>
        <XtxBreadItem>{{List.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 轮播图 -->
      <XtxSlider :sliders='BannerList' style='height:500px'></XtxSlider>
      <div class="sub-list">
    <h3>全部分类</h3>
    <ul>
      <li v-for="item in List.children" :key="item.id">
        <a href="javascript:;">
          <img v-imgLazy="item.picture" >
          <p>{{item.name}}</p>
        </a>
      </li>
    </ul>
     </div>
     <div class="ref-goods" v-for="item in List.children" :key="item.id">
    <div class="head">
      <h3>-  {{item.name}}  -</h3>
      <XtxMore to="/" />
    </div>
    <div class="body">
      <RouterLink  class="goods-item" v-for="it in item.goods" :key="it.id" :to="'/goods/'+it.id">
        <img
          :src="it.picture"
          alt=""
        />
        <p class="name ellipsis">{{it.name}}</p>
        <p class="desc ellipsis">{{it.desc}}</p>
        <p class="price">&yen;{{it.price}}</p>
      </RouterLink>
    </div>
    </div>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import { useBanner } from '@/composition/userBanner.js'
import { GetCateListApi } from '@/api/CategoryApi.js'
export default {
  name: 'XtxCategory',
  setup () {
    const route = useRoute()
    const id = route.params.id
    const List = ref([])
    GetCateListApi(id).then((res) => {
      console.log(res)
      List.value = res.result
    })
    const BannerList = useBanner()
    // 解决跳转同一页面数据不刷新
    onBeforeRouteUpdate((to) => {
      console.log('地址变化')
      GetCateListApi(to.params.id).then((res) => {
        List.value = res.result
      })
    })
    return { id, BannerList, List }
  }
}
</script>
<style scoped lang="less">
.goods-item {
    display: block;
    width: 220px;
    padding: 20px 30px;
    text-align: center;
    img {
      width: 160px;
      height: 160px;
    }
    p {
      padding-top: 10px;
    }
    .name {
      font-size: 16px;
    }
    .desc {
      color: #999;
      height: 29px;
    }
    .price {
      color: @priceColor;
      font-size: 20px;
    }
  }
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      padding: 0 65px 30px;
      flex-wrap: wrap;
    }
  }
}
</style>
